package com.stillalive.springaop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
//@Aspect
@Component
// 切面类
public class AspectDemo1 {
    // 定义切点
    @Pointcut("execution(* com.stillalive.springaop.controller.*.*(..))")
    public void pt(){};

    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint pjp) {
        log.info("目标方法执行前....");
        // 执行目标方法
        Object result = null;
        try {
            result = pjp.proceed();
        }
        // 这里只能使用Throwable，因为proceed中声明的是Throwable，
        // 如果使用Exception会导致该方法抛出的某些异常不能被正确捕获，导致程序出错
        catch (Throwable e) {
            log.error("do Around throws exceptions...");
        }
        log.info("目标方法执行后....");
        return result;
    }

    @Before("pt()")
    public void doBefore(){
        log.info("doBefore....");
    }

    @After("pt()")
    public void deAfter(){
        log.info("deAfter....");
    }

    @AfterReturning("pt()")
    public void doAfterReturning(){
        log.info("doAfterReturning....");
    }

    @AfterThrowing("pt()")
    public void doAfterThrowing(){
        log.info("doAfterThrowing....");
    }
}
